<template>
    <div>
        <div>
            <div>
                <el-button type="primary" @click="addBtn" size="mini">添加商品</el-button>
				<el-button  @click="saveGoods" size="mini">确定退货</el-button>
            </div>
        </div>
        <br>
        <div class="searchForm">
            <div>
                退货日期： 
				<el-date-picker
				      size="small"
				      v-model="form.buyer_date"
					  value-format="yyyy-MM-dd"
				      type="date"
				      placeholder="选择日期时间">
				    </el-date-picker>
            </div>
            <div>
                制单人： 
				<el-input size="small" v-model="form.buyer_person" 
				placeholder="请输入制单人"  
				style="width:150px;"></el-input>
            </div>
            <div>
                <div style="display: flex;">
                	<div>
                		客户：
                	</div>
                	<div>
                		<selectCustomer 
						  holder="请输入客户名称"
						 :supp="form.supplier"
						 @getCustomerName="getCustomerName" />
                	</div>
                </div>
            </div>
			<div>
			    串号： <el-input size="small" 
				v-model="goods_code" 
				@input="chagenCode"
				ref="goods_code"
				placeholder="请扫描串号"  
				style="width:300px;"></el-input>
			</div>
        </div>
        <br>
        <el-table  :data="list"  
		     ref="multipleTable"
			 border  
			 style="width: 1200px"
			 @selection-change="handleSelectionChange"
			 @cell-click="cellClick"
			>
			
			<el-table-column
			      type="selection"
			      width="55">
			    </el-table-column>
			<!-- <el-table-column   align="center"  width="200px"   label="串号">
				<template slot-scope="scope">
				   <div>
				   	  <el-input @change="changeCodeInput(scope)"  size="mini" v-model="scope.row.code"></el-input>
				   </div>
				</template>
			</el-table-column> -->	
            <el-table-column   align="center"  width="200px"   
				label="商品名称">
				<template slot-scope="scope">
				   <div>
						<el-input disabled  size="mini" v-model="scope.row.goods_name"></el-input>
				   </div>
				</template>
			</el-table-column>
            <el-table-column  align="center" width="130px"  label="数量">
				<template slot-scope="scope">
				   <div>
						<el-input disabled @input="cumpMoney(scope)"  size="mini" v-model="scope.row.goods_number"></el-input>
				   </div>
				</template>
			</el-table-column>
             <el-table-column   align="center"     
				label="单价">
				<template slot-scope="scope">
				   <div>
						<el-input @input="cumpMoney(scope)" size="mini" v-model="scope.row.price"></el-input>
				   </div>
				</template>
			</el-table-column>
            <el-table-column   align="center"  width="130px"   label="金额">
				<template slot-scope="scope">
				   <div>
						<el-input disabled  size="mini" v-model="scope.row.money"></el-input>
				   </div>
				</template>
			</el-table-column>
			
			<el-table-column   align="center"  width="180px"   label="备注">
				<template slot-scope="scope">
				   <div>
						<el-input  size="mini" v-model="scope.row.remark"></el-input>
				   </div>
				</template>
			</el-table-column>
		  <el-table-column  label="操作"  fixed="left"  align="center">
                <template slot-scope="scope">
                    <i class="el-icon-delete" style="cursor: pointer;" 
					@click="delRow(scope)"></i>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
	
	import selectCustomer from "@/components/selectCustomer"
    export default {
        components:{
			selectCustomer
        },

        data(){
            return {
				goods_code:'',
                form:{
                    buyer_date:"",
					buyer_person:"",
					supplier:"",
					logistics_number:""
                },
				supperData:[],
				rowData:{}
            }
        },
        mounted(){
			this.form.buyer_date = this.$utils.formatNowDate(2)
			this.form.buyer_person = sessionStorage.getItem('user_name')
			this.addBtn()
            document.title = '批发退货'
			this.$refs.goods_code.focus()
        },
        methods:{
			chagenCode(){
					let params = {
						code:this.goods_code,
						status:2
					}
					this.$post('/buyerSys/getGoodsForCode',params,res=>{
						let result = res.data.data
						if(result){
							this.goods_code = ''
							let obj = {
								id:undefined,
								goods_name:result.goods_name,
								goods_number:1,
								price:'',
								money:"",
								code:result.code,
								remark:result.remark,
								codeArray:[result.code],
							}
							if(this.list.length==0){
								this.list.push(obj)
							}else{
								var isPush = true
								for(let item of this.list){
									if(item.goods_name == result.goods_name){
										item.goods_number++
										item.codeArray.push(result.code)
										// item.codeArray = []
										isPush = false
									}else if(item.goods_name=='' && this.list.length==1){
										this.list = []
										setTimeout(()=>{
											this.list.push(obj)
										},200)
										isPush = false
									}
								}
								if(isPush){
									this.list.push(obj)
								}
							}
						}
					})
			},
			cumpMoney(scope){
				let index = scope.$index
				let goods_number = scope.row.goods_number
				let price = scope.row.price
				if(goods_number && price){
					this.list[index].money = goods_number * price
				}else{
					this.list[index].money = 0
				}
			},
			getCustomerName(val){
				this.form.supplier = val
			},
			changeCodeInput(scope){
				let index = scope.$index
				let len = this.list.length
				if(len-1 == index){
					this.addBtn()
				}
				let code = scope.row.code
				this.$post('/buyerSys/getGoodsForCode',{code,status:2},res=>{
					let result = res.data
					if(result.data){
						if(result.data.id){
							this.list[index].goods_name = result.data.goods_name
							this.list[index].goods_number = 1
							// this.list[index].price = result.data.price
							// this.list[index].money = result.data.money
							// this.form.supplier= result.data.supplier
							
						}
					}
				})
			},
			
			handleSelectionChange(val){
				this.selectGoods = [...val]
			},
            delRow(scope){
				let index = scope.$index
				this.list.splice(index,1)
            },
            saveGoods(){
				let order_number = this.$utils.generateRandomString(2)
				if(this.selectGoods.length==0) return this.$message.error('请选择商品')
				let cid = sessionStorage.getItem('cid')
				for(let item of this.selectGoods){
					item.cid = cid
					item.order_number = order_number
					item.buyer_date = this.form.buyer_date
					item.buyer_person = this.form.buyer_person
					item.supplier = this.form.supplier
					item.logistics_number = this.form.logistics_number
					item.type = 3
				}
				
				let newArr = [...this.selectGoods]
				let newArr1 = []
				for(let item of newArr){
					if(item.goods_number==1){
						item.code = item.codeArray[0]
						item.type = 2
					}
					if(item.goods_number>1){
						for(let i=0;i<item.goods_number;i++){
							let obj = {
								buyer_date:item.buyer_date,
								buyer_person:item.buyer_person,
								cid:item.cid,
								code:item.codeArray[i],
								goods_name:item.goods_name,
								goods_number:1,
								id:item.id,
								logistics_number:item.logistics_number,
								money:item.money,
								order_number:item.order_number,
								price:item.price,
								remark:item.remark,
								supplier:item.supplier,
								type:2
							}
							newArr1.push(obj)
						}
					}
				}
				newArr.push(...newArr1)
				newArr = newArr.filter((item,i)=>{
					return item.goods_number==1
				})
				
				this.$post('/buyerSys/addEditBuyer',newArr,res=>{
					let result = res.data
					if(result.code == 200){
						this.list = []
						this.$message.success('操作成功')
						this.form = {
						    buyer_date:"",
							buyer_person:"",
							supplier:"",
							logistics_number:""
						}
					}
					
				})
			},
            addBtn(){ 
                let obj = {
					id:undefined,
                    goods_name:"",
					goods_number:1,
					price:"",
					money:"",
					code:'',
					remark:"老串号退货"
                }
                this.list.push(obj)
            },
        }
    }
</script>
